package second.binarytree;


import java.util.ArrayList;
import java.util.List;

public class Solution25 {
    TreeNode pre = null;
    List<Integer> res;

    int count = 0;
    int maxCount = 0;

    public int[] findMode(TreeNode root) {
        res = new ArrayList<>();
        getMax(root);
        int[] resArr = new int[res.size()];
        for (int i = 0; i < res.size(); i++) {
            resArr[i] = res.get(i);
        }
        return resArr;
    }

    public void getMax(TreeNode root) {
        if (root == null) return;

        getMax(root.left);

        if (pre != null) {
            if (pre.val == root.val) {
                count++;
            } else {
                count = 1;
            }
        } else {
            count++;
        }
        if (count > maxCount) {
            res.clear();
            res.add(root.val);
            maxCount = count;
        } else if (count == maxCount) {
            res.add(root.val);
        }
        pre = root;

        getMax(root.right);
    }
}

